package com.hkw.servlet;

import com.hkw.model.Student;
import com.hkw.service.IStudentService;
import com.hkw.service.impl.StudentServiceImpl;
import com.hkw.utils.PagesUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 显示所有学生信息的servlet
 */
@WebServlet(name = "StudentServlet",urlPatterns = "/studentlist")
public class StudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取主页面分页栏那传来的nowpage的值
        String nowpage = request.getParameter("nowpage");
        StudentServiceImpl studentService = null;
        Integer nowpage1 = null;
        //如果为空，设置当前页为1，因为是第一次访问这个请求
        if(nowpage == null){
            studentService = new StudentServiceImpl(1);
            //往request里设置属性
            request.setAttribute("nowpage",1);
        }else{
            //不为空，则往request里设置nowpage1属性
            nowpage1 = Integer.parseInt(nowpage);
            studentService = new StudentServiceImpl(nowpage1);
            request.setAttribute("nowpage",nowpage1);
        }
        //调用接口服务
        List<Student> list = studentService.list();
        //将返回结果放入request里
        request.setAttribute("reaPage",studentService.getPagesUtils().getNowPage());
        request.setAttribute("stList",list);
        //携带数据跳转到main.jsp，显示所有用户信息
        request.getRequestDispatcher("main.jsp").forward(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
